﻿using System;
using System.IO;
using System.Web;
using Rabbit.Logic;
using RabbitManage.Code;
using Raven.Imports.Newtonsoft.Json;

namespace RabbitManage.Action
{
    public partial class ShopDetailEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies[Constant.CookieName];
            string shopId = cookie.Values[Constant.CookieUid];

            Stream postData = Request.InputStream;
            StreamReader sRead = new StreamReader(postData);
            string postContent = sRead.ReadToEnd();
            sRead.Close();

            if (!string.IsNullOrEmpty(postContent))
            {
                try
                {
                    Address address = JsonConvert.DeserializeObject<Address>(postContent);
                    Shop item = JsonConvert.DeserializeObject<Shop>(postContent);
                    Shop shop = RabbitUse.GetShopByShopId(Db.Store, shopId);
                    shop.Location = address;
                    shop.NickName = item.NickName;
                    shop.DiliveryCostDescription = item.DiliveryCostDescription;
                    shop.FreeShipAt = item.FreeShipAt;

                    RabbitUse.AddOrUpdateShop(Db.Store, shop);

                    Response.Write("true");
                }
                catch (Exception ex)
                {
                    Response.Write(ex.Message);
                }
            }
            else
            {
                Response.Write("false");
            }
        }
    }
}